package com.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.dao.SourceTypeDAO;
import com.rev.Source;
import com.rev.Sourcetype;
import com.rev.SourcetypeHome;
import com.rev.Targettype;
import com.util.HibernateUtil;

public class SourceTypeDAOImpl implements SourceTypeDAO{

	private SourcetypeHome sth = new SourcetypeHome();
	public boolean insert(Sourcetype sourceType) {
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction  transaction = session.beginTransaction();
			sth.attachDirty(sourceType);
			transaction.commit();
			session.close();
			return true;
		} catch (Exception e) {}
		return false;
	}

	public boolean update(Sourcetype sourceType) {
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction  transaction = session.beginTransaction();
			sth.attachDirty(sourceType);
			transaction.commit();
			session.close();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	public boolean delete(Sourcetype sourceType) {
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction  transaction = session.beginTransaction();
			sth.delete(sourceType);
			transaction.commit();
			session.close();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	public Sourcetype findById(int id) {
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			Sourcetype s = sth.findById(id);
			session.getTransaction().commit();
			session.close();
			return s;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	public Sourcetype findByRef(String ref) {
		return null;
	}

	public List<Sourcetype> findAll() {

		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			List list = sth.findByExample(new Sourcetype());
			session.getTransaction().commit();
			session.close();
			return list;
		} catch (Exception e) {
		}
		return null;
	}

}
